//
//  PPKeychainManager.m
//  ppss
//
//  Created by zhongrui on 2020/12/3.
//

#import "PPKeychainManager.h"
#import <YYKit/YYKit.h>

static NSString *const  PPDataBaseKeychainServiceName = @"com.zhongrui.ppss";
static YYKeychainAccessible const PPKeychainDefaultAccessible = YYKeychainAccessibleWhenUnlockedThisDeviceOnly;

@implementation PPKeychainManager

//数据是否存在钥匙串中
- (BOOL)containsKeychainWithParams:(NSDictionary *)params{

    NSString *keyStr =  params[@"key"];
    return [self containsKeychainForKey:keyStr accessible:PPKeychainDefaultAccessible];
}

- (BOOL)containsKeychainForKey:(NSString *)key accessible:(YYKeychainAccessible)accessible{
    YYKeychainItem *item = [YYKeychainItem new];
    item.service = PPDataBaseKeychainServiceName;
    item.account = key;
    item.accessible = accessible;
    NSError *error;
    YYKeychainItem *result = [YYKeychain selectOneItem:item error:&error];
    BOOL exit = result?YES:NO;
    return exit;
}

//存储数据
- (BOOL)setKeychainWithParams:(NSDictionary *)params{

    id password =  params[@"password"];

    NSString *keyStr =  params[@"key"];
    
    return [self setPassword:password Key:keyStr accessible:PPKeychainDefaultAccessible];
}

- (BOOL)setPassword:(id)password Key:(NSString *)key accessible:(YYKeychainAccessible)accessible{
    if (!password || !key) {
        return NO;
    }
    NSString *passwordStr = @"";
    NSError *error;
    if (![password isKindOfClass:[NSString class]]) passwordStr = [password jsonStringEncoded];
    if ([password isKindOfClass:[NSString class]]) passwordStr = password;
    
    YYKeychainItem *item = [YYKeychainItem new];
    item.service = PPDataBaseKeychainServiceName;
    item.account = key;
    item.accessible = accessible;
    YYKeychainItem *result = [YYKeychain selectOneItem:item error:&error];
    if (result) {
        result.password = passwordStr;
        return [YYKeychain updateItem:result error:&error];
    } else {
        item.password = passwordStr;
        return [YYKeychain insertItem:item error:&error];
    }
}

//获取数据
- (id)getKeychainWithParams:(NSDictionary *)params{

    NSString *keyStr =  params[@"key"];

    return [self getPasswordForKey:keyStr accessible:PPKeychainDefaultAccessible];
}

- (id)getPasswordForKey:(NSString *)key accessible:(YYKeychainAccessible)accessible{
    if (!key) {
        return nil;
    }
    YYKeychainItem *item = [YYKeychainItem new];
    item.service = PPDataBaseKeychainServiceName;
    item.account = key;
    item.accessible = accessible;
    NSError *error;
    YYKeychainItem *result = [YYKeychain selectOneItem:item error:&error];
    NSString *passwordStr = result.password;
    id password = [passwordStr jsonValueDecoded];
    return password?:passwordStr;
}

//移除数据
- (BOOL)removeKeychainWithParams:(NSDictionary *)params{
    NSString *keyStr =  params[@"key"];

    return [self removePasswordForKey:keyStr accessible:PPKeychainDefaultAccessible];
}

- (BOOL)removePasswordForKey:(NSString *)key accessible:(YYKeychainAccessible)accessible{
    if (!key) {
        return NO;
    }
    YYKeychainItem *item = [YYKeychainItem new];
    item.service = PPDataBaseKeychainServiceName;
    item.account = key;
    item.accessible = accessible;
    NSError *error;
    BOOL success = [YYKeychain deleteItem:item error:&error];
    return success;
}

@end
